package com.fit.utils.se;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 股票算法模型(初稿)
 * @author hcl
 * @since 2021/4/11
 */
public class Test {


//    public static void main(String[] args) {
//
//        //起始金额
//        Double d = 30000D;
//        //固定金额
//        Double fixed = d;
//        //数量
//        Integer num = 0;
//
//        try {
//            List<List<Record<String>>> list = ExcelUtil.readExcel(new FileInputStream("D:\\new_tdx\\T0002\\export\\000608_day.xlsx"),0,null,null);
//            Double lastLv = null;
//            Double lastHong = null;
//            Double lQian = null;
//            Double fQian = null;
//            for (int i = 0; i < list.size(); i++) {
//                List<Record<String>> row = list.get(i);
//                System.out.println();
//                row.forEach(coll->{
//                    System.out.print( StringUtil.replenish(coll.getString(),12,StringUtil.BEFORE,' '));
//                });
//                System.out.println();
//
//                if(i > 3 && row.size() > 23 && row.get(21).getString().trim().length() != 0 && row.get(22).getString().length() != 0 ){
//                    Double lv = row.get(21).getDouble();
//                    Double hong = row.get(22).getDouble();
//                    System.out.println( lv >= hong ? "绿":"红");
//                    if(lv != null && hong != null){
//                        if(lv.equals(hong)){
//                            lv += 0.001D;
//                        }
//                        if(hong > lv){
//                            lv += 0.05D;
//                        }
//
//                        Double lastQian = row.get(4).getDouble();
//                        lQian = lastQian;
//                        if(fQian == null){
//                            fQian = lastQian;
//                        }
//                        if(lastLv != null && lastHong != null){
//                            if(lastHong > lastLv && hong < lv){
//                                //卖出
//                                System.out.print("卖出:");
//                                if(num >= 100){
//                                    System.out.print(num);
//                                    d = d + (lastQian * num);
//                                    num = 0;
//                                }else{
//                                    System.out.print("数量不足");
//                                }
//                            }else if(lastHong < lastLv && hong > lv){
//                                //买入
//                                System.out.print("买入:");
//                                if(lastQian * 100 <= d){
//                                    int n = (int)Math.floor(d / (lastQian * 100));
//                                    n = n * 100;
//                                    num += n;
//                                    System.out.print(n);
//                                    d -= lastQian * n;
//                                }else{
//                                    System.out.print("金额不足");
//                                }
//                            }else{
//                                //无操作
//                                System.out.print("无操作");
//                            }
//
//
////                            if(hong < lv){
////                                //卖出
////                                System.out.print("卖出:");
////                                if(num >= 100){
////                                    System.out.print(num);
////                                    d = d + (lastQian * num);
////                                    num = 0;
////                                }else{
////                                    System.out.print("数量不足");
////                                }
////                            }else if(hong > lv){
////                                //买入
////                                System.out.print("买入:");
////                                if(lastQian * 100 <= d){
////                                    int n = (int)Math.floor(d / (lastQian * 100));
////                                    n = n * 100;
////                                    num += n;
////                                    System.out.print(n);
////                                    d -= lastQian * n;
////                                }else{
////                                    System.out.print("金额不足");
////                                }
////                            }else{
////                                //无操作
////                                System.out.print("无操作");
////                            }
//
//
//                        }
//
//                        lastLv = lv;
//                        lastHong = hong;
//                    }else{
//                        System.out.println(row.get(0)+"：没数据; ");
//                    }
//
//                }else{
//                    lastLv = null;
//                    lastHong = null;
//                }
//                System.out.println("\r\n现状 金额："+d+",数量："+num);
//            }
//
//            System.out.println("操作后最终金额："+(d+(lQian*num)));
//
//
//            int nx = (int)Math.floor(fixed / (fQian * 100));
//            nx = nx*100;
//            fixed -= (fQian * nx);
//            System.out.println("不操作金额："+(fixed+(lQian*nx)));
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }




    public static void main(String[] args) {

        //起始金额
        Double d = 30000D;
        //固定金额
        Double fixed = d;
        //数量
        Integer num = 0;
        //买入日期
        Integer date = 0;

        Double lQian = null;
        Double fQian = null;

        try {
            List<List<Record<String>>> list = ExcelUtil.readExcel(new FileInputStream("C:\\Work\\fit\\0.0.2\\fit-parent\\fit-utils\\fit-utils-se\\src\\main\\java\\com\\fit\\utils\\se\\000608_day.xlsx"),0,null,null);
            //历史数据
            List<List<Record<String>>> history = new ArrayList<>(list.size());
            //历史买卖
            List<Integer> historyBuying = new ArrayList<>(list.size());

            AtomicInteger ato = new AtomicInteger(0);
            for (List<Record<String>> row : list) {
                ato.incrementAndGet();
                System.out.println();
                row.forEach(coll->{
                    System.out.print( StringUtil.replenish(coll.getString(),12,StringUtil.BEFORE,' '));
                });
                System.out.println();
                if(ato.get() > 4 && row.size() > 20){

                    Double lastQian = row.get(4).getDouble();
                    lQian = lastQian;
                    if(fQian == null){
                        fQian = lastQian;
                    }

                    Integer bljj = isBljjBuy(row,history);
                    Integer zjlr = isZjlrBuy(row,history);
                    Set<Integer> results = new HashSet<>();
                    results.add(bljj);
                    results.add(zjlr);

                    //不打开效果较好，可以多测试几次
//                    Integer volatility = isVolatilityBuy(results,historyBuying);
//                    results.add(volatility);

                    if(results.contains(0)){
                        //卖出
                        System.out.print("卖出:");
                        if(num >= 100){
                            System.out.print(num);
                            d = d + (lastQian * num);
                            num = 0;
                        }else{
                            System.out.print("数量不足");
                        }
                        //卖出
                        historyBuying.add(0);
                    }else{
                        //买入
                        System.out.print("买入:");
                        if(lastQian * 100 <= d){
                            int n = (int)Math.floor(d / (lastQian * 100));
                            n = n * 100;
                            num += n;
                            System.out.print(n);
                            d -= lastQian * n;
                        }else{
                            System.out.print("金额不足");
                        }
                        //买入
                        historyBuying.add(1);
                    }
                    history.add(row);

                    System.out.println("\r\n现状 金额："+d+",数量："+num);
                }
            }

            System.out.println("操作后最终金额："+(d+(lQian*num)));


            int nx = (int)Math.floor(fixed / (fQian * 100));
            nx = nx*100;
            fixed -= (fQian * nx);
            System.out.println("不操作金额："+(fixed+(lQian*nx)));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 捕捞季节是否可以买入
     * @param row 当前行数据
     * @param history 历史数据
     * @return 买入还是卖出，1买入，0卖出
     * @author hcl
     * @since 2021/4/11
     */
    public static Integer isBljjBuy(List<Record<String>> row,List<List<Record<String>>> history){

        Record<String> lvStr = row.get(21);
        Record<String> hongStr = row.get(22);
        if(lvStr.getString().trim().length() == 0 || hongStr.getString().trim().length() == 0){
            return 0;
        }
        Double lv = lvStr.getDouble();
        Double hong = hongStr.getDouble();
        if(lv.equals(hong)){
            lv += 0.001D;
        }

        if(hong > lv){
            //买入
            return 1;
        }
        //卖出
        return 0;
    }


    /**
     * 资金流入是否可以买入
     * @param row 当前行数据
     * @param history 历史数据
     * @return 买入还是卖出，1买入，0卖出
     * @author hcl
     * @since 2021/4/11
     */
    public static Integer isZjlrBuy(List<Record<String>> row,List<List<Record<String>>> history){
        if(history.size() == 0){
            return 0;
        }
        List<Record<String>> lastRow = history.get(history.size() - 1);
        Record<String> trafficStr = row.get(42);
        Record<String> lastTrafficStr = lastRow.get(42);
        if(trafficStr.getString().trim().length() == 0 || lastTrafficStr.getString().trim().length() == 0){
            return 0;
        }

        Double traffic = trafficStr.getDouble();
        Double lastTraffic = lastTrafficStr.getDouble();



        if(traffic.equals(lastTraffic)){
            traffic -= 0.01D;
        }
        if(traffic > lastTraffic){
            //买入
            return 1;
        }
        //卖出
        return 0;
    }


    /**
     * 今日是否波动，
     * @param historyBuying
     * @return 买入还是卖出，1买入，0卖出
     * @author hcl
     * @since 2021/4/11
     */
    public static Integer isVolatilityBuy(Set<Integer> dayResults,List<Integer> historyBuying){
        Integer dayBuy = dayResults.contains(0) ? 0 : 1;
        if(historyBuying.size() > 2 && dayBuy == 1){
            if(!historyBuying.get(historyBuying.size()-1).equals(historyBuying.get(historyBuying.size() - 2))){
                return 0;
            }
        }
        return dayBuy;
    }



}
